home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 1995 March / SOFM_Mar1995.bin / mac / SRI / General Interest / Programming / J / Examples / convert.js next >
Encoding:
Text File  |  1993-12-02  |  2.8 KB  |  107 lines  |  [TEXT/????]

  1.    NB. conversion utilities
  2.    NB.
  3.    NB.  av         convert between characters and indices
  4.    NB.  chop       chop array into boxed list
  5.    NB.  detab      remove tab stops
  6.    NB.  dfh        decimal from hex
  7.    NB.  exportfmt  export format numeric data
  8.    NB.  hfd        hex from decimal
  9.    NB.  vfm        vector from matrix
  10.    NB.  mfv        matrix from vector
  11.    NB.  quote      quote text
  12.    NB.  tolower    to lower case
  13.    NB.  toupper    to upper case
  14.    NB.  toCR       convert to CR separator
  15.    NB.  toLF       convert to LF separator
  16.    NB.  toCRLF     convert to CRLF separator
  17.    NB.  toSUB      convert to 1{a. separator
  18.    NB.  toLFTAB    convert TAB to LF
  19.    
  20.    t=. 'av chop detab dfh exportfmt hfd vfm mfv quote'
  21.    t=. t,' subs tolower toupper toCR toLF toCRLF toSUB toLFTAB'
  22.    SCRIPTNAMES=: t
  23.    
  24.    hex=.    '0123456789ABCDEF'
  25.    dfh=:    16&#. @ (hex&i.) f.
  26.    hfd=:    hex&({~) @ (16 16&#:) f.
  27.    
  28.    NB. av
  29.    NB. e.g   av 'abcde'
  30.    av=: '({&a.)`(a.&i.) @. (2&=@(3!:0)) y.' :0
  31.    
  32.    TAB   =. 9{a.
  33.    LF    =. 10{a.
  34.    CR    =. 13{a.
  35.    CRLF  =. 13 10{a.
  36.    EAV   =. 255{a.
  37.    
  38.    subs=.    [. & ((((e.&) (# i.@#@)) (@])) })   
  39.    crlf=.    #~ -.@(CRLF&E.)                     
  40.    
  41.    toLF=:    LF subs CR @ crlf f.
  42.    toCR=:    CR subs LF @ crlf f.
  43.    toSUB=:   (1{a.) subs (CR,LF) @ crlf f.
  44.    toCRLF=:  2&}.@;@(((CR&,)&.>)@<;.1@(LF&,)@toLF)
  45.    toLFTAB=: LF subs TAB f.
  46.    
  47.    NB. quote
  48.    NB. quote 'Pete''s Place'
  49.    a=. ''''
  50.    quote=: (a&,@(,&a))@ (#~ >:@(=&a))
  51.    
  52.    NB. chop
  53.    NB. chop character vector or matrix into boxed list
  54.    NB. x. is optional delimiter, default blank
  55.    NB. e.g.  chop ": 10 20 30
  56.    NB.       chop ": i. 5 4
  57.    chop=: 0 : 0
  58. ' ' chop y.
  59. :
  60. $.=. >(2=#$y.){ ({.;}.)$.
  61. (<'') -.~ (y e.x.) <;._2 y=. y.,{.x.
  62. |: &.> y -. {: y=. (*./y e.x.) <;._2 |: y=. y.,"1 [ 2${.x.
  63. )
  64.    
  65.    NB. detab
  66.    NB. remove tabs from character string
  67.    NB. left argument is tab width, default 4
  68.    a=. 0 : 0
  69. 4 detab y.
  70. :
  71. NB. replace tabs in text
  72. tab=. 9{a.
  73. r=. y.
  74. L0) i=. >: r i. tab
  75. r [ $.=. $.#~ i<:#r
  76. r=. ((x. * >. i % x.){.!.' ' }:i{.r) , i}.r
  77. $.=. L0
  78. )
  79.    detab=: a"1 f.
  80.    
  81.    NB. exportfmt
  82.    NB. format numeric array of rank 0 1 or 2 as required
  83.    NB. by other software, e.g. Excel 
  84.    NB. numbers are separated by TAB, rows by CR
  85.    exportfmt=: 0 : 0
  86. 'rc'=. $ ,.y.
  87. dat=. ,y.
  88. dat=. ":&.>dat
  89. sep=. 1|.((#dat)$c{.1) { 9 10{a.
  90. dat=. ;dat,&.>sep
  91. '-' subs '_' dat
  92. )
  93.    
  94.    NB. vector <> matrix, lines separated by LF
  95.    mfv=: '];._2 y,LF #~ LF ~: {:y=. toLF y.' :0
  96.    vfm=: '}:(,|."1 [ 1,.-. *./\"1 |."1 y.='' '')#,y.,.LF' :0
  97.    
  98.    NB. tolower
  99.    j=. '(y.i.~''ABCDEFGHIJKLMNOPQRSTUVWXYZ'',a.)'
  100.    j=.    j,'{''abcdefghijklmnopqrstuvwxyz'',a.'
  101.    tolower=: j : ''
  102.    
  103.    NB. toupper
  104.    j=. '(y.i.~''abcdefghijklmnopqrstuvwxyz'',a.)'
  105.    j=.    j,'{''ABCDEFGHIJKLMNOPQRSTUVWXYZ'',a.'
  106.    toupper=: j : ''
  107.